using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib.Prepare
{
    [Serializable()]
    public class AreaPub : BusinessBase<AreaPub>
    {
        #region Business Methods
        int m_id;
        Guid m_buildingid;
        string m_category = string.Empty;
        string m_name = string.Empty;
        double m_pubArea;
        double m_nonpubArea;
        string m_carrier = string.Empty;
        string m_memo = string.Empty;

        public int ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public Guid Buildingid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_buildingid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_buildingid != value)
                {
                    m_buildingid = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Category
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_category;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_category != value)
                {
                    m_category = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_name != value)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }
        public double PubArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_pubArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_pubArea != value)
                {
                    m_pubArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double NonpubArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_nonpubArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_nonpubArea != value)
                {
                    m_nonpubArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Carrier
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_carrier;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_carrier != value)
                {
                    m_carrier = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static AreaPub NewAreaPub()
        {
            // TODO: change to use new keyword if not loading defaults
            return new AreaPub();
        }

        internal static AreaPub GetAreaPub(SafeDataReader dr)
        {
            return new AreaPub(dr);
        }

        public static void InsertAreaPub(string category, string name, double pub, double nonpub, string carrier,Guid buildingID)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AInsertAreaPub");

            db.AddInParameter(dbcom, "category", DbType.String, category);
            db.AddInParameter(dbcom, "name", DbType.String, name);
            db.AddInParameter(dbcom, "pubArea", DbType.Double, pub);
            db.AddInParameter(dbcom, "nonpubArea", DbType.Double, nonpub);
            db.AddInParameter(dbcom, "carrier", DbType.String, carrier);
            db.AddInParameter(dbcom, "buildingid", DbType.Guid, buildingID);
            db.AddInParameter(dbcom, "memo", DbType.String, null);


            db.ExecuteNonQuery(dbcom);
        }

        private AreaPub()
        {
            MarkAsChild();
        }

        private AreaPub(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetInt32("id");

            m_buildingid = dr.GetGuid("buildingid");
            m_category = dr.GetString("category");
            m_name = dr.GetString("name");
            m_pubArea = dr.GetDouble("pubArea");
            m_nonpubArea = dr.GetDouble("nonpubArea");
            m_carrier = dr.GetString("carrier");
            m_memo = dr.GetString("memo");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GInsertAreaPub");
            db.AddOutParameter(dbcom, "id", DbType.Int32, m_id);
            DoInsertUpdate(dbcom);
            m_id = (Int32)db.GetParameterValue(dbcom, "id");
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AUpdateAreaPub");

            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);
            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            
            db.AddInParameter(dbcom, "buildingid", DbType.Object, m_buildingid);
            db.AddInParameter(dbcom, "category", DbType.String, m_category);
            db.AddInParameter(dbcom, "name", DbType.String, m_name);
            db.AddInParameter(dbcom, "pubArea", DbType.Single, m_pubArea);
            db.AddInParameter(dbcom, "nonpubArea", DbType.Single, m_nonpubArea);
            db.AddInParameter(dbcom, "carrier", DbType.String, m_carrier);
            db.AddInParameter(dbcom, "memo", DbType.String, m_memo);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GDeleteAreaPub");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }

        #endregion
    }//end of class	
}//end of namespace 

